package controllers

import (
	"admin-panel/cache"
	"admin-panel/models"
	"admin-panel/pkg"
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
)

type User struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (u *User) Login() {

	username := u.GetString("username")
	password := u.GetString("password")
	if username == "" || password == "" {
		u.Data["json"] = Res{
			Code: 0,
			Msg:  "用户名和密码不能为空",
		}
		u.ServeJSON()
		return
	}
	loginErrorRedisKey := "login_error" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, loginErrorRedisKey).Int()
	if errCount >= 3 {
		u.Data["json"] = map[string]interface{}{

			"Code": 0,
			"Msg":  "密码错误3次禁止登录",
		}
		u.ServeJSON()
		return

	}
	var err error
	var user models.User
	err = models.DB.Table("users").Where("username=?", username).First(&user).Debug().Error
	if err != nil {
		u.Data["json"] = map[string]interface{}{

			"Code": 0,
			"Msg":  "用户查询失败",
		}
		u.ServeJSON()
		return
	}
	if user.Password != pkg.Md5Str(password) {
		cache.Rdb.Incr(cache.Ctx, loginErrorRedisKey)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, loginErrorRedisKey, 3, time.Hour*2)
			u.Data["json"] = map[string]interface{}{
				"Code": 0,
				"Msg":  "密码错误3次,封禁2个小时",
			}
			u.ServeJSON()
			return
		}
		u.Data["json"] = map[string]interface{}{
			"Code": 0,
			"Msg":  "密码错误",
		}
		u.ServeJSON()
		return
	}
	cache.Rdb.Del(cache.Ctx, loginErrorRedisKey) //清空错误次数
	u.Data["json"] = map[string]interface{}{
		"Code": 200,
		"Msg":  "登录成功",
	}
	u.ServeJSON()
	return
}

func (u *User) List() {

	dataListKey := "data_list"
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataListKey).Result()
	var user []models.User
	if cacheJson == "" {
		models.DB.Model(&models.User{}).Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, dataListKey, jsonData, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &user)
		if err != nil {
			u.Data["json"] = Res{
				Code: 0,
				Msg:  "缓存数据获取失败",
			}
			u.ServeJSON()
			return
		}
		u.Data["json"] = map[string]interface{}{
			"Code": 200,
			"Msg":  "缓存数据获取成功",
			"Data": user,
		}
		u.ServeJSON()
		return

	}

}
func (u *User) Detail() { //详情缓存

	id, _ := u.GetInt("id")

	dataListKey := "data_id_s" + strconv.Itoa(id)
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataListKey).Result()
	fmt.Println(cacheJson)
	var user models.User
	if cacheJson == "" {
		//从Mysql查数据
		models.DB.Model(&models.User{}).Where("id=?", id).First(&user)
		//redis保存数据
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, dataListKey, jsonData, time.Hour*2)

	} else {

		//从redis得到数据
		err := json.Unmarshal([]byte(cacheJson), &user)
		if err != nil {
			u.Data["json"] = Res{
				Code: 0,
				Msg:  "缓存数据获取失败",
			}
			u.ServeJSON()
			return
		}

	}
	u.Data["json"] = map[string]interface{}{
		"Code": 200,
		"Msg":  "缓存数据获取成功",
		"Data": user,
	}
	u.ServeJSON()
	return

}
